package src.Base;

import src.Writer;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;

public class ErrorLog {
    public static ErrorLog instance = new ErrorLog();
    ArrayList<Error> errors = new ArrayList<>();
    public void addError(int line, String type){
        Error error = new Error(line, type);
        System.out.println("ErrLog:"+type+" line:"+line);
        errors.add(error);
    }
    public boolean repeatErrorA(int line){
        return errors.get(errors.size() - 1).getLine() == line;
    }
    public void writeErrors(){
        if(errors.size() == 0){
            System.out.println("No Error!");
            return;
        }
        File file = new File("error.txt");
        errors.sort(new Comparator<Error>() {
            @Override
            public int compare(Error o1, Error o2) {
                return o1.getLine() - o2.getLine();
            }
        });
        for(Error error:errors){
           Writer.instance.write("error.txt", error.getLine() + " " + error.getType() +"\n");
        }
        System.exit(0);
    }
}
